﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DungeonDelver.Common
{
    public class BoardCell<T,U>:IBoardCell<T,U>
    {

        public IBoardColumn<T, U> BoardColumn
        {
            get;
            set;
        }

        public int Column
        {
            get 
            {
                return BoardColumn.Column;
            }
        }

        public int Row
        {
            get 
            {
                foreach (var row in BoardColumn.Rows)
                {
                    if (BoardColumn[row] == this)
                    {
                        return row;
                    }
                }
                throw new IndexOutOfRangeException();
            }
        }

        private Dictionary<T, IBoardCell<T, U>> neighbors = new Dictionary<T, IBoardCell<T, U>>();

        public IBoardCell<T, U> GetNeighbor(T theDirection)
        {
            if (neighbors.ContainsKey(theDirection))
            {
                return neighbors[theDirection];
            }
            else
            {
                return null;
            }
        }

        public void SetNeighbor(T theDirection, IBoardCell<T, U> theNeighbor)
        {
            if (theNeighbor == null)
            {
                if (neighbors.ContainsKey(theDirection))
                {
                    neighbors.Remove(theDirection);
                }
            }
            else
            {
                if (neighbors.ContainsKey(theDirection))
                {
                    neighbors[theDirection] = theNeighbor;
                }
                else
                {
                    neighbors.Add(theDirection, theNeighbor);
                }
            }
        }

        public bool HasNeighbor(T theDirection)
        {
            return neighbors.ContainsKey(theDirection);
        }

        public U Contents
        {
            get; set;
        }

        public BoardCell()
        {
        }
    }
}
